package ui.fragment;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Media;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.localvideo.R;

import java.io.File;
import java.util.ArrayList;

import adapter.VideoListAdapter;
import bean.VideoBean;
import ui.activity.MainActivity;
import ui.activity.VideoPlayerActivity;
import utils.LogUtils;


//视频播放列表页面
public class VideoListFragment extends BaseFragment{
    private static final String TAG ="VideoListFragment";
    private ListView lv;
    private Activity activity= getActivity();
    private static final String[] sLocalVideoColumns={
            Media._ID,
            Media.DATA,
            Media.SIZE,
            Media.DISPLAY_NAME,
            Media.TITLE,
            Media.DURATION
    };
    //
    private int thisPosition;//记录长按的是第几个item
    private myDialog menuDialog;
    private String thisTitle;
    private String thisPath;
    private String thisTime;
    private String thisSize;
    private ArrayList<VideoBean> thislist;

    @Override
    public int getLayout() {
        return R.layout.fragment_video_list;

    }

    @Override
    public void initView() {
        lv = (ListView)findViewById(R.id.lv);
        //设置dialog
        activity = getActivity();
        View viewDialog = getLayoutInflater().inflate(R.layout.dialog_menu,null);
        menuDialog = new myDialog(activity,0,0,viewDialog,R.style.DialogTheme);
        View viewdelDialog = getLayoutInflater().inflate(R.layout.dialog_delete1,null);
        final myDialog delDialog = new myDialog(activity,0,0,viewdelDialog,R.style.DialogTheme);
        View viewshowDialog = getLayoutInflater().inflate(R.layout.dialog_show,null);
        final myDialog showDialog = new myDialog(activity,0,0,viewshowDialog,R.style.DialogTheme);

        //显示信息
        TextView dialogllShow = menuDialog.findViewById(R.id.dialog_show);
        dialogllShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView title = showDialog.findViewById(R.id.dialog_show_title) ;
                TextView time =  showDialog.findViewById(R.id.dialog_show_time) ;
                TextView size =  showDialog.findViewById(R.id.dialog_show_size) ;
                TextView path =  showDialog.findViewById(R.id.dialog_show_path) ;
                title.setText("视频标题：  "+thisTitle);
                time.setText("视频长度:   "+thisTime);
                size.setText("视频大小:   "+thisSize);
                path.setText("视频路径:   "+thisPath);
                menuDialog.dismiss();
                showDialog.show();
            }
        });
        //播放
        TextView dialogPlay = menuDialog.findViewById(R.id.dialog_play);
        dialogPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
                //传视频的数据，但是因为不可以直接传bean,所以要让bean实现serializable接口
//                VideoBean bean =VideoBean.getInstanceFromCursor(cursor);
//                intent.putExtra("bean",bean);
                //不用上面那个了，换成传集合
                intent.putExtra("list",thislist);
                intent.putExtra("position",thisPosition);
                //跳转
                startActivity(intent);
            }
        });
        //显示删除
        TextView dialogDelete = menuDialog.findViewById(R.id.dialog_delete);
        dialogDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialog.dismiss();
                delDialog.show();
            }
        });
        Button delCancel = delDialog.findViewById(R.id.dialog_bottom_cancel);
        delCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delDialog.dismiss();
            }
        });
        Button delYes = delDialog.findViewById(R.id.dialog_bottom_yes);
        delYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delPath();
                Toast.makeText(activity,"删除成功",Toast.LENGTH_SHORT).show();
                delDialog.dismiss();
            }
        });
    }

//删除视频
    private void delPath() {
       File file = new File(thisPath);
        boolean flag = false;

        if (file.isFile() && file.exists()){
            flag =  file.delete();
            System.gc();
            LogUtils.e("删除视频",thisPath);
            LogUtils.e("删除视频","存在文件");
           LogUtils.e("删除视频","删除状态："+flag);
       }
    }
    //获得点击的数据
    private void getData(){
        ContentResolver resolver = getActivity().getContentResolver();
        //必须要添加ID，否则报错
        Uri targetUrl = Media.EXTERNAL_CONTENT_URI;
        final Cursor delcursor = resolver.query(targetUrl,
                sLocalVideoColumns,
                null,null,null);
        ArrayList<VideoBean> list = VideoBean.getBeanFormCursor(delcursor);
        VideoBean videoBean = list.get(thisPosition);

        thisTitle = videoBean.getTitle();
        thisPath = videoBean.getData();
        thisTime = formatTime(videoBean.getTime());
        thisSize = Formatter.formatFileSize(activity,videoBean.getSize());

        thislist = VideoBean.getBeanFormCursor(delcursor);

    }
    //格式化时间
    private String formatTime(int gettime){
        String showTime;
        final int HOUR = 60*60*1000;
        final int MIN = 60*1000;
        final int SEC = 1000;
        int time = gettime;
        int hour = time / HOUR;
        int min = time % HOUR / MIN;
        int sec = time %MIN /SEC;
        if (hour >=1){//00:00:00
            showTime =  String.format("%02d:%02d:%02d",hour,min,sec);
        }else{//00:00
            showTime =  String.format("%02d:%02d",min,sec);
        }
        return showTime;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        //获取视频数据
        ContentResolver resolver = getActivity().getContentResolver();
        //必须要添加ID，否则报错
        Uri targetUrl = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

        final Cursor cursor = resolver.query(targetUrl,
                sLocalVideoColumns,
                null,null,null);

        lv.setAdapter(new VideoListAdapter(getActivity(),cursor));


        //点击列表点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);

                ArrayList<VideoBean> list = VideoBean.getBeanFormCursor(cursor);
                intent.putExtra("list",list);
                intent.putExtra("position",position);
                //跳转
                startActivity(intent);
            }
        });
        //长按列表事件
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //dialog
                thisPosition = position;
                getData();
                menuDialog.show();
                return true;//这里如果是true的话click就会被吃掉

            }
        });



    }

}
